using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Patrol : MonoBehaviour
{
    private Animator animator;
    public GameObject battleArena;
    public GameObject battlePanels;

    #region 巡逻
    /// <summary>
    /// 轨道半径
    /// </summary>
    private float orbitDistance = 3f;
    /// <summary>
    /// 角速度
    /// </summary>
    private float orbitSpeed = 1f;
    /// <summary>
    /// 平滑时间
    /// 增大这个值会减慢移动速度
    /// </summary>
    private float smoothTime = 2f;

    /// <summary>
    /// 当前角度
    /// </summary>
    private float currentAngle = 0f;

    /// <summary>
    /// 当前速度
    /// </summary>
    private Vector2 currentVelocity;
    #endregion

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        #region 巡逻
        Vector3 center = transform.localPosition + new Vector3(1, 0, 0);
        // 位移
        currentAngle += orbitSpeed * Time.deltaTime;
        Vector2 orbitOffset = new Vector2(
            Mathf.Cos(currentAngle) * orbitDistance,
            Mathf.Sin(currentAngle) * orbitDistance
        );
        Vector2 targetPosition = (Vector2)center + orbitOffset;
        // transform.localPosition = Vector2.SmoothDamp(transform.localPosition, targetPosition, ref currentVelocity, smoothTime);
        // 动画
        Vector2 lookDirection = (Vector2)transform.localPosition - targetPosition;
        animator.SetFloat(Defines.LookX, lookDirection.x);
        animator.SetFloat(Defines.LookY, lookDirection.y);
        #endregion
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag(Defines.MainCharacterName))
        {
            this.battleArena.SetActive(true);
            this.battlePanels.SetActive(true);
            GameManager.Instance.CurrentMonster = gameObject;
            LunaStatusManager.Instance.LunaState = LunaState.Battle;
        }
    }
}
